package com.supor.aiot.basket.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.basket.R;
import com.supor.aiot.basket.adapter.BasketDialogAdapter;
import com.supor.aiot.basket.adapter.IngredientDialogAdapter;
import com.supor.aiot.basket.bean.CommonIngredientBean;
import com.supor.aiot.basket.bean.IsMainIngredients;
import com.xuhao.didi.socket.common.interfaces.utils.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class ButtomDialog extends Dialog {
    public ButtomDialog(Context context, int themeResId) {
        super(context, themeResId);
    }
    public static class Params {
        private final List<BottomMenu> menuList = new ArrayList<>();
        private View.OnClickListener cancelListener;
        private View.OnClickListener confirmListener;
        private CharSequence menuTitle;
        private Context context;
        private List<CommonIngredientBean> mainIngredients = new ArrayList<>();
        private List<CommonIngredientBean> commonIngredients = new ArrayList<>();
        private List<CommonIngredientBean> otherIngredients = new ArrayList<>();
    }
    public static class Builder {
        private boolean canCancel = true;
        private boolean shadow = true;
        private final Params p;
        public Builder(Context context) {
            p = new Params();
            p.context = context;
        }
        public Builder setCanCancel(boolean canCancel) {
            this.canCancel = canCancel;
            return this;
        }
        public Builder setShadow(boolean shadow) {
            this.shadow = shadow;
            return this;
        }
        public Builder setTitle(CharSequence title) {
            this.p.menuTitle = title;
            return this;
        }
        public Builder addMenu(String text, View.OnClickListener listener) {
            BottomMenu bm = new BottomMenu(text, listener);
            this.p.menuList.add(bm);
            return this;
        }

        public Builder setMainIngredient(List<CommonIngredientBean> ingredientsList) {
            this.p.mainIngredients.clear();
            this.p.mainIngredients.addAll(ingredientsList);
            return this;
        }

        public Builder setOtherIngredient(List<CommonIngredientBean> ingredientsList) {
            this.p.otherIngredients.clear();
            this.p.otherIngredients.addAll(ingredientsList);
            return this;
        }

        public Builder setCommonIngredient(List<CommonIngredientBean> ingredientsList) {
            this.p.commonIngredients.clear();
            this.p.commonIngredients.addAll(ingredientsList);
            return this;
        }

        public Builder addMenu(int textId, View.OnClickListener listener) {
            return addMenu(p.context.getString(textId), listener);
        }

        public Builder setCancelListener(View.OnClickListener cancelListener) {
            p.cancelListener = cancelListener;
            return this;
        }

        public Builder setConfirmListener(View.OnClickListener confirmListener) {
            p.confirmListener = confirmListener;
            return this;
        }

        public ButtomDialog create() {
            final ButtomDialog dialog = new ButtomDialog(p.context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
            Window window = dialog.getWindow();
            window.setWindowAnimations(R.style.Animation_Bottom_Rising);
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            window.setGravity(Gravity.BOTTOM);
            View view = LayoutInflater.from(p.context).inflate(R.layout.dialog_ingredient_list, null);
            ImageView btnCancel = view.findViewById(R.id.iv_list_cancle);
            TextView tvTitle = view.findViewById(R.id.tv_basket_title);
            TextView tvConfirm = view.findViewById(R.id.tv_confirm_list);
            if (p.confirmListener!=null)tvConfirm.setOnClickListener(p.confirmListener);
            RecyclerView rvMainIngredient = view.findViewById(R.id.rv_main_ingredient);
            LinearLayout linearLayoutMain = view.findViewById(R.id.ll_main_ingredient);
            RecyclerView rvOtherIngredient = view.findViewById(R.id.rv_other_ingredient);
            LinearLayout linearLayoutOther = view.findViewById(R.id.ll_other_ingredient);
            RecyclerView rvCommonIngredient = view.findViewById(R.id.rv_common_ingredient);
            LinearLayout linearLayoutCommon = view.findViewById(R.id.ll_common_ingredient);

            if (p.mainIngredients.size()>0){
                IngredientDialogAdapter mainAdapter = new IngredientDialogAdapter(p.mainIngredients,p.context);
                LinearLayoutManager layoutManager = new LinearLayoutManager(p.context);
                rvMainIngredient.setLayoutManager(layoutManager);
                rvMainIngredient.setAdapter(mainAdapter);
                linearLayoutMain.setVisibility(View.VISIBLE);
            }else {
                linearLayoutMain.setVisibility(View.GONE);
            }

            if (p.otherIngredients.size()>0){
                IngredientDialogAdapter mainAdapter = new IngredientDialogAdapter(p.otherIngredients,p.context);
                LinearLayoutManager layoutManager = new LinearLayoutManager(p.context);
                rvOtherIngredient.setLayoutManager(layoutManager);
                rvOtherIngredient.setAdapter(mainAdapter);
                linearLayoutOther.setVisibility(View.VISIBLE);
            }else {
                linearLayoutOther.setVisibility(View.GONE);
            }

            if (p.commonIngredients.size()>0){
                IngredientDialogAdapter mainAdapter = new IngredientDialogAdapter(p.commonIngredients,p.context);
                LinearLayoutManager layoutManager = new LinearLayoutManager(p.context);
                rvCommonIngredient.setLayoutManager(layoutManager);
                rvCommonIngredient.setAdapter(mainAdapter);
                linearLayoutCommon.setVisibility(View.VISIBLE);
            }else {
                linearLayoutCommon.setVisibility(View.GONE);
            }


            ViewGroup.LayoutParams lpItem = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            ViewGroup.MarginLayoutParams lpDivider = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
            lpDivider.setMargins(50,0,50,0);

            boolean hasTitle = !TextUtils.isEmpty((String) p.menuTitle);
            if (hasTitle) {
                tvTitle.setText(p.menuTitle);
            }

            if (p.cancelListener != null) {
                btnCancel.setOnClickListener(p.cancelListener);
            } else {
                btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(canCancel);
            dialog.setCancelable(canCancel);
            return dialog;
        }
    }
    private static class BottomMenu {
        public String funName;
        public View.OnClickListener listener;
        public BottomMenu(String funName, View.OnClickListener listener) {
            this.funName = funName;
            this.listener = listener;
        }
    }
}
